import { ElMessage } from 'element-plus'

type MessageType = 'success' | 'error' | 'warning' | 'info'
type MessageOptions = {
	duration?: number
	grouping?: boolean
	plain?: boolean
	[key: string]: any
}

const types: MessageType[] = ['success', 'error', 'warning', 'info']

/**
 * * @description 消息提示
 * * @param {String} message 消息内容
 * * @param {String} type 消息类型，默认info，可选值：success/warning/info/error
 * * @param {Object} args 其他参数，详见：https://element-plus.org/zh-CN/component/message.html
 */
const messageInfo = (type: MessageType, message: any, args: MessageOptions = {}) => {
	if (!types.includes(type)) {
		console.error(`Invalid message type! Like: ${types.join(' | ')}`)
		return
	}
	const { duration = 2000, grouping = true, plain = true } = args
	ElMessage({
		type,
		message,
		duration,
		grouping,
		plain,
		...args
	})
}

export const $message = {
	success: (message: any, args?: object) => messageInfo('success', message, args),
	warning: (message: any, args?: object) => messageInfo('warning', message, args),
	info: (message: any, args?: object) => messageInfo('info', message, args),
	error: (message: any, args?: object) => messageInfo('error', message, args = {})
}